package com.ttjj.entity.map;

import com.alibaba.fastjson.annotation.JSONField;
import com.ttjj.enums.EnumDirection;

import java.text.MessageFormat;

/**
 * 大图坐标
 * 注意position与Coordinate 之间的转化横纵坐标需要对调
 */
public class Position {
    protected final int x;
    protected final int y;
    /**
     *  hash
     */
    @JSONField(serialize = false)
    protected final int hash;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
        this.hash = x * 28 + y;
    }

    public EnumDirection getVerticalDirectFrom(Position pos) {
        if (this.x < pos.x) {
            return EnumDirection.TOP;
        } else {
            return this.x > pos.x ? EnumDirection.DOWN : EnumDirection.STOP;
        }
    }

    public EnumDirection getHorizontalDirectFrom(Position pos) {
        if (this.y < pos.y) {
            return EnumDirection.LEFT;
        } else {
            return this.y > pos.y ? EnumDirection.RIGHT : EnumDirection.STOP;
        }
    }

    public Position stepByDirect(EnumDirection direct) {
        if (direct == EnumDirection.TOP) {
            return new Position(this.x - 1, this.y);
        } else if (direct == EnumDirection.DOWN) {
            return new Position(this.x + 1, this.y);
        } else if (direct == EnumDirection.LEFT) {
            return new Position(this.x, this.y - 1);
        } else {
            return direct == EnumDirection.RIGHT ? new Position(this.x, this.y + 1) : this;
        }
    }

    public Coordinate toCoordinate() {
        return new Coordinate(this.y * 50 + 25, this.x * 50 + 25);
    }

    public String print() {
        return MessageFormat.format("({0},{1})", this.x, this.y);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj != null && this.getClass() == obj.getClass()) {
            Position other = (Position)obj;
            return this.hash == other.hash;
        } else {
            return false;
        }
    }

    public int hashCode() {
        return this.hash;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    public int getHash() {
        return this.hash;
    }

    public String toString() {
        return "Position(x=" + this.getX() + ", y=" + this.getY() + ", hash=" + this.getHash() + ")";
    }
}
